

function show_draft_box(pageIndex){
    var _box=$("#draft_box_window"),_list=$("#draft_box_list");
    _box.fadeIn(500);_list.html("<span class='load-tag'></span>");
    setTimeout(function(){
        pageIndex = typeof(pageIndex) == "undefined" ? 0 : pageIndex;
        var containerId = "draft_pagination";
        doRequest("/ajax/get_drafts",{pageIndex:pageIndex,stamptime:Math.random()},false,function(resp){
            var totalCount = resp.data.totalCount;
            var str = "";
            if(totalCount == 0){
                str += "<div class='tip-null' style='height:160px;'></div>";
            }else{
                var htmlData = resp.data.htmlData;
                str += htmlData;
            }
            _list.html(str);
            createPaginationModel({containerId:containerId,perNum:5,currIndex:resp.data.pageIndex,totalCount:totalCount,num:6,flag:false},show_draft_box);
        },function(data){});
        
    },1000);
}

function delete_get_drafts($id,obj){
    var _show=$(obj).parents(".popup-box").find(".del-box-show");
    doGetRequest("/ajax/del_drafts",{draftsId:$id,stamptime:Math.random()},false,function(resp){
        $("#draft_number_box").html(resp.totalCount);
        show_draft_box(0);_show.text("删除成功").fadeIn(500);
        setTimeout(function(){
            _show.fadeOut(500);
        },1000);
    },function(data){});
}

$(function(){
    var art_cates_box=$("#art_cates_box"),art_tags_box=$("#art_tags_box");
    if(typeof(art_cates_box[0])!="undefined"){
        if(art_cates_box.attr("data-flag") != 0){
            art_cates_box.children("li:first").addClass("on");
        }
        art_cates_box.find("li").click(function(){
            $(this).addClass("on").siblings().removeClass("on");
        });
    }
    if(typeof(art_tags_box[0])!="undefined"){
        $("body").delegate("#art_tags_box a","click",function(){
           $(this).hasClass('on') ? $(this).removeClass('on') : $(this).addClass('on');
           check_art_tags(art_tags_box);
        });
    }
    $("#seach_art_key_tag").keyup(function(e){
        if(e.keyCode == 38 || e.keyCode == 40){
            select_tag(e.keyCode);
            return;
        }else if(e.keyCode == 13){
            if(($("#seach_art_tag_box a.active"))[0] !== undefined){
                selected_tag($("#seach_art_tag_box a.active"));
                return;
            }
        }
        
        var _self=$(this),_key_tag=_self.val(),_seach_art_tag_box=$("#seach_art_tag_box");
        if(_key_tag == "") {_seach_art_tag_box.css("display","none");return;};
        _seach_art_tag_box.css("display","block").html("<span class='load-tag'></span>");
        doGetRequest("/ajax/seach_arts_tag",{tag_key:_key_tag},true,function(res){
            var html = res.seachRes;
            if(html == ""){
                _seach_art_tag_box.html("<span class='empty'>匹配为空</span>");
            }else{
                setTimeout(function(){
                    _seach_art_tag_box.html("").html(html);
                },500);
            }
        },function(res){});
    });
    $("body").delegate("#seach_art_tag_box a","click",function(e){
        selected_tag($(this));
    });
    
    function check_art_tags(obj){
        if((obj.find("a[class='on']")).length > 5){
            $("#art_tags_error").css("display","block");
        }else{
            $("#art_tags_error").css("display","none");
        }
    }
    function check_art_is_on(obj){
        var t=0;
        var tt=setInterval(function(){
            t++;
            $(obj).hasClass('on') ? $(obj).removeClass('on') : $(obj).addClass('on');
            if(t == 4) {clearInterval(tt);}
        },300);
        return;
    }
    var _art_titile_obj=$("#articles_title"),_art_content_obj=$("#articles_content"),_art_cat_obj=$("#art_cates_box"),
    _art_tags_obj=$("#art_tags_box"),_email_notice_checkbox=$("#email_notice_select"),_T_art_draft_btn=$("._T_art_draft"),
    _T_art_publish_btn=$("._T_art_publish"),_T_art_modify_btn=$("._T_art_modify"),_T_art_preview_btn=$("._T_art_preview"),_draft_number_obj=$("#draft_number");
    _T_art_publish_btn.on("click",function(){
        var _self=$(this),_title=$.trim(_art_titile_obj.val()),_content=_art_content_obj.val(),_cat_id=check_art_cat(_art_cat_obj),_tags=check_tags(_art_tags_obj),is_email=_email_notice_checkbox.hasClass("active") ? 1 : 0,params={},
        _unvalid=_self.attr("data-unvalid"),_url=_self.attr("data-action-url"),_draft=_draft_number_obj.attr("data-val");
        if(!check_title(_title) || !check_content(_content)){return;};
        if(!_cat_id){layer.msg("至少选择一个文章板块哦",{icon:0,time:2000,shift:6,offset:300});return;}
        if(!_tags){layer.msg("至少选择一个文章标签哦",{icon:0,time:2000,shift:6,offset:300});return;}
        params.artTitle=_title,params.artContent=_content,params.artCat=_cat_id,params.artTag=_tags,params.is_email=is_email,params.draftNnumber=_draft;
        _self.attr("disabled","disabled").css("cursor","not-allowed").val(_unvalid);
        doRequest(_url,params,true,function(data){
            layer.confirm(data.errorMsg, {shift:6,offset:300,closeBtn:0,
                btn: ['前去查看','继续发布'],icon:6,title:"小6提示" //按钮
                }, function(){
                    window.location.href = data.url;
                }, function(){
                    window.location.reload();
            });
        },function(data){
            _self.removeAttr("disabled").css("cursor","pointer").val("确认发布");
            layer.msg(data.errorMsg,{icon:0,time:2000,offset:300});
        });
    });
    _T_art_modify_btn.on("click",function(){
        var _self=$(this),_title=$.trim(_art_titile_obj.val()),_content=_art_content_obj.val(),_cat_id=check_art_cat(_art_cat_obj),_tags=check_tags(_art_tags_obj),is_email=_email_notice_checkbox.hasClass("active") ? 1 : 0,params={},
        _unvalid=_self.attr("data-unvalid"),_url=_self.attr("data-action-url"),_draft=_draft_number_obj.attr("data-val"),_article_id=_self.attr("data-aid");
        if(!check_title(_title) || !check_content(_content)){return;};
        if(!_cat_id){layer.msg("至少选择一个文章板块哦",{icon:0,time:2000,shift:6,offset:300});return;}
        if(!_tags){layer.msg("至少选择一个文章标签哦",{icon:0,time:2000,shift:6,offset:300});return;}
        params.artTitle=_title,params.artContent=_content,params.artCat=_cat_id,params.artTag=_tags,params.is_email=is_email,params.draftNnumber=_draft,params.articleId=_article_id;
        _self.attr("disabled","disabled").css("cursor","not-allowed").val(_unvalid);
        doRequest(_url,params,true,function(data){
            layer.confirm(data.errorMsg, {shift:6,offset:300,closeBtn:0,
                btn: ['前去查看','继续发布'],icon:6,title:"小6提示" //按钮
                }, function(){
                    window.location.href = data.url;
                }, function(){
                    window.location.reload();
            });
        },function(data){
            _self.removeAttr("disabled").css("cursor","pointer").val("确认发布");
            layer.msg(data.errorMsg,{icon:0,time:2000,offset:300});
        });
    });
    _T_art_draft_btn.on("click",function(){
        var _self=$(this),_title=$.trim(_art_titile_obj.val()),_content=_art_content_obj.val(),_cat_id=check_art_cat(_art_cat_obj),_tags=check_tags(_art_tags_obj),is_email=_email_notice_checkbox.hasClass("active") ? 1 : 0,params={},
        _unvalid=_self.attr("data-unvalid"),_url=_self.attr("data-action-url"),_draft=_draft_number_obj.attr("data-val");
        if(!check_title(_title) || !check_content(_content)){return;};
        if(!_cat_id){layer.msg("至少选择一个文章板块哦",{icon:0,time:2000,shift:6,offset:300});return;}
        if(!_tags){layer.msg("至少选择一个文章标签哦",{icon:0,time:2000,shift:6,offset:300});return;}
        params.artTitle=_title,params.artContent=_content,params.artCat=_cat_id,params.artTag=_tags,params.is_email=is_email,params.draftNnumber=_draft;
        _self.attr("disabled","disabled").css("cursor","not-allowed").text(_unvalid);
        doRequest(_url,params,true,function(data){
            $("#draft_number_box").text(data.boxNum);
            _self.removeAttr("disabled").css("cursor","pointer").text("保存草稿箱");
            layer.msg("已保存到草稿箱",{icon:1,time:2000,offset:300});
        },function(data){
            _self.removeAttr("disabled").css("cursor","pointer").text("保存草稿箱");
            layer.msg(data.errorMsg,{icon:0,time:2000,offset:300});
        });
    });
    _T_art_preview_btn.on("click",function(){
        var _self=$(this),_title=$.trim(_art_titile_obj.val()),_content=_art_content_obj.val(),_cat_id=check_art_cat(_art_cat_obj),_tags=check_tags(_art_tags_obj),is_email=_email_notice_checkbox.hasClass("active") ? 1 : 0,params={},
        _unvalid=_self.attr("data-unvalid"),_url=_self.attr("data-action-url"),_draft=_draft_number_obj.attr("data-val");
        if(!check_title(_title) || !check_content(_content)){return;};
        if(!_cat_id){layer.msg("至少选择一个文章板块哦",{icon:0,time:2000,shift:6,offset:300});return;}
        if(!_tags){layer.msg("至少选择一个文章标签哦",{icon:0,time:2000,shift:6,offset:300});return;}
        params.artTitle=_title,params.artContent=_content,params.artCat=_cat_id,params.artTag=_tags,params.is_email=is_email,params.draftNnumber=_draft;
        _self.attr("disabled","disabled").css("cursor","not-allowed").text(_unvalid);
        doRequest(_url,params,true,function(data){
            _self.removeAttr("disabled").css("cursor","pointer").text("保存预览");
            window.open(data.url);
        },function(data){
            _self.removeAttr("disabled").css("cursor","pointer").text("保存预览");
            layer.msg(data.errorMsg,{icon:0,time:2000,offset:300});
        });
    });
    _art_titile_obj.keyup(function(){
        var _self=$(this),_title=$.trim(_self.val());
        _total_len=check_str_len(_title);
        $(".title-count").text(_total_len);
    });
    function check_art_cat(obj){
        obj = obj.find("li[class='on']");
        if(typeof(obj[0]) && obj.length > 0){
            return obj.children("a").attr("date-id");
        }else{
            return false;
        }
    }
    function check_tags(obj){
        var str="",obj = obj.find("a[class='on']");
        if(typeof(obj[0]) && obj.length > 0){
           obj.each(function(index,data){
               if((index+1) < 6){
                   str += $(this).attr("data-name")+"_"+$(this).attr("data-tid")+",";
               }
           });
           return str.substring(0,str.length-1);
        }else{
            return "";
        }
        
    }
    function check_title(str){
        var total_len,ch_len,en_len;
        if(str == ""){layer.msg("请输入标题",{icon:0,time:2000,shift:6,offset:300});return false;}
        total_len=str.length,en_len=(str.replace(/[\u4E00-\u9FA5]/g,"")).length,ch_len=total_len-en_len,en_len=parseInt(en_len/2);
        if((ch_len+en_len) < 10){layer.msg("标题不能<span style='font-size:16px;color:#e66d61;'>小于10个字</span>哦",{icon:0,time:2000,shift:6,offset:300});return false;}
        return true;
    }
    
    function check_content(str){
        if(str == ""){layer.msg("内容不能为空哦",{icon:0,time:2000,shift:6,offset:300});return false;}
        return true;
    }
    
    function select_tag(code){
        $("#seach_art_tag_box").focus();
        var tar = $("#seach_art_tag_box a");
        if(tar[0] === undefined){return;}
        index = $("#seach_art_tag_box a.active").index();
        num = 0;
        if(code == 38){
            if(index < 1){return}
            num = index - 1;
        }else if(code == 40){
            if((index + 1) == tar.length){return;}
            num = index + 1;
        }
        var obj = tar.eq(num);
        obj.addClass("active").siblings().removeClass("active");
        $("#seach_art_tag_box").scrollTop(obj[0].scrollHeight * num);
    }
    
    function selected_tag(obj){
        var _self=obj,_flag=$("#art_tags_box").find("a[data-tid="+_self.attr("seach-tid")+"]");
        $("#seach_art_tag_box").html("").hide();$("#seach_art_key_tag").val("");
        if(typeof(_flag[0]) != "undefined"){
            if(_flag.hasClass("on")){
                check_art_is_on(_flag);
            }else{
                _flag.addClass("on");
            }
        }else{
            $("#art_tags_box").append("<a href='javascript:void(0);' data-name='"+_self.attr("seach-name")+"' data-tid='"+_self.attr("seach-tid")+"' class='on'>"+_self.attr("seach-name")+"</a>");
        }
        check_art_tags($("#art_tags_box"));
    }
});

